﻿using System;
using System.Collections;
using System.Collections.Generic;
using LogSystem;
using Pathfinding;
using Pathfinding.RVO;
using UnityEngine;
using UnityEngine.Serialization;
using RootMotion.Dynamics;
using Yoozoo.Gameplay.Liberty.AI.Node;

namespace Yoozoo.Gameplay.Liberty.AI
{
    public class LibertyNpcSeeker : AISeeker
    {
        public Seeker Seeker;
        public CharacterController CharacterController;
        public Collider triggerCollider;

        public RVOController RvoController;
        private bool onTargetReached = true;
        private LibertyCarBirthManager _carBirthManager => LibertyAIUtils.AIManager.CarBirthManager;
        private AIEntity aiEntity;

        public PhysicsCasterCollection npcCaster;
        private void Awake()
        {
            base.OnAwake();
            aiPath.onReached += OnTargetReached;
        }

        private void OnDestroy()
        {
            aiPath.onReached -= OnTargetReached;
        }

        private void OnTargetReached()
        {
            onTargetReached = true;
        }

        public override bool ReachedEndOfPath()
        {
            return onTargetReached;
        }
        

        public override void SetDestination(Vector3 destination)
        {
            if (Vector3.Distance(destination, Transform.position) < 0.01f)
            {
                onTargetReached = true;
                return;
            }
            onTargetReached = false;
            base.SetDestination(destination);
        }

        public override void SetMaxSpeed(float speed)
        {
            savedMaxSpeed = speed;
            aiPath.maxSpeed = speed;
        }

        public override void SetPriority(float priority)
        {
            RvoController.priority = priority;
        }

        public void SetCurAiEntity(AIEntity _aiEntity)
        {
            aiEntity = _aiEntity;
        }

        // public float bargeMinSpeed = 2;
        // public float deadSpeed = 5;
        //
        //
        // public void OnTriggerEnter(Collider collider)
        // {
        //     if (aiEntity == null || aiEntity.puppetMaster == null)
        //     {
        //         return;
        //     }
        //     CarEntity carEntity = _carBirthManager.GetCarEntity(collider);
        //     if (carEntity != null)
        //     {
        //         float carSpeed = carEntity.carController.getForwardVelocity();
        //         if (aiEntity.data.isAlive &&
        //             carSpeed > bargeMinSpeed && 
        //             aiEntity.data.npcStatus != NpcStatus.VehicleBarge)
        //         {
        //             StopAllCoroutines();
        //             StartCoroutine(VehicleBarge(carEntity));
        //         }
        //     }
        // }
        //
        // [Range(0,4)]
        // public float deadDelayTime = 2f;
        // private IEnumerator VehicleBarge(CarEntity carEntity)
        // {
        //     // aiEntity.viewer.animationAgent.EnterBattleState(BattleState.None);
        //     aiEntity.viewer.animationAgent.EnterAimingState(AimingState.None);
        //     aiEntity.viewer.animationAgent.SetDisableOperate(true);
        //         
        //     //NpcStatus lastNpcStatus = aiEntity.data.npcStatus;
        //     aiEntity.data.npcStatus = NpcStatus.VehicleBarge;
        //     
        //     Vector3 carPos = carEntity.position;
        //     Vector3 carDir = carEntity.forward;
        //     Vector3 carRight = carEntity.right;
        //     
        //     float angle = Vector3.Angle(carDir, transform.position - carPos);
        //     float v = Vector3.Cross(carDir, transform.position - carPos).y;
        //
        //     Animator animator = aiEntity.viewer.animator;
        //     LocomotionAgent locomotionAgent = LocomotionAgent;
        //     locomotionAgent.vehicleBargeTime = 0;
        //     // 关掉寻路逻辑与行为树
        //
        //     CharacterController.enabled = false;
        //     triggerCollider.enabled = false;
        //     
        //     if (angle < 25)
        //     {
        //         Vector3 bargeDir = transform.forward;
        //         if (v > 0)
        //         {
        //             //右边
        //             bargeDir = -(carDir + carRight).normalized;
        //         }
        //         else
        //         {
        //             //左边
        //             bargeDir = -(carDir - carRight).normalized;
        //         }
        //
        //         float angle2 = Vector3.Angle(carDir, animator.transform.forward);
        //         if (angle2 > 90)
        //         {
        //             animator.CrossFade("vehicle_dead",0.05f,0);
        //         }
        //         else
        //         {
        //             animator.CrossFade("vehicle_dead2",0.05f,0);
        //         }
        //          
        //         locomotionAgent.isVehicleBarge = true;
        //         locomotionAgent.bargeDir = bargeDir;
        //         
        //         yield return new WaitForSeconds(deadDelayTime);
        //         aiEntity.puppetMaster.state = PuppetMaster.State.Dead;
        //         aiEntity.puppetMaster.enabled = true;
        //         locomotionAgent.isVehicleBarge = false;
        //         yield return new WaitForSeconds(4);
        //     }
        //     else if(angle < 135)
        //     {
        //         animator.CrossFade("vehicle_scratch",0.05f,0);
        //         locomotionAgent.isVehicleScratch = true;
        //         locomotionAgent.bargeDir = (transform.position - carPos).normalized;
        //         
        //         yield return new WaitForSeconds(1.5f);
        //         locomotionAgent.isVehicleScratch = false;
        //         yield return new WaitForSeconds(1.2f);
        //     }
        //     
        //     CharacterController.enabled = true;
        //     triggerCollider.enabled = true;
        //     aiEntity.data.npcStatus = aiEntity.data.defaultNpcStatus;    // 还原到撞车之前的状态
        //     aiEntity.viewer.animationAgent.SetDisableOperate(false);
        //     LibertyAIUtils.OnResetDefaultStatus?.Invoke(aiEntity.data.uniqueId,aiEntity.data.defaultNpcStatus); 
        // }
        //
        //
        // public void OnNpcDeath()
        // {
        //     Debug.Log("OnNpcDeath");
        //     if (aiEntity.data.isAlive)
        //     {
        //         aiEntity.puppetMaster.state = PuppetMaster.State.Alive;
        //     }
        // }
    }
}